import React, { Component } from 'react';
import { StoreProvider, msg } from 'iflux2';
import AppStore from './store';

import Search from './component/search';
import OrderList from './component/order-list';


@StoreProvider(AppStore)
export default class App extends Component {
    componentDidMount() {
        let type = (this.props.params.type
            && this.props.params.type.toLowerCase() === 'unpay')
            ? 'UNPAY_ALL'
            : 'ALL';
        //订单状态，默认全部订单
        this.props.store.setOrderStatus(type);
        this.props.store.setOrderType(this.props.params.type);
        this.props.store.queryExpressCompany();
        this.queryOrderList();

    }

    componentWillReceiveProps(nextProps) {
        if (this.props.params.type != nextProps.params.type) {
            let type = nextProps.params.type
            && nextProps.params.type.toLowerCase() === 'unpay' ? 'UNPAY_ALL' : 'ALL';
            //重置搜索参数
            this.props.store.changeSearchParams({});
            this.props.store.setOrderStatus(type);
            this.props.store.setOrderType(nextProps.params.type);
            msg.emit("page_changed");
            this.props.store.resetPagination();
            this.queryOrderList();
        }
    }


    render() {
        return (
            <div className="order-list">
                <Search />
                <OrderList />
            </div>
        )
    }

    queryOrderList = ()=>{
        msg.emit('setLoading', true);
        this.props.store.queryOrderList().then(()=>{
            msg.emit('setLoading', false);
        }).catch(()=>{
            msg.emit('setLoading', false);
        });
    }
}

